package br.com.vpsa.previdordevendas.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.CustomNumberEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import br.com.vpsa.previdordevendas.model.entity.Declinio;
import br.com.vpsa.previdordevendas.model.service.DeclinioService;
import br.com.vpsa.previdordevendas.model.service.EntidadeService;
import br.com.vpsa.previdordevendas.model.util.BaseUtil;
import br.com.vpsa.previdordevendas.model.util.CalendarUtils;
import br.com.vpsa.previdordevendas.model.util.MoedaUtil;

@Controller
@RequestMapping("/declinio")
public class DeclinioController 
{
	@Autowired
	private EntidadeService entidadeService;
	
	@Autowired
	private DeclinioService declinioService;
	
	@InitBinder  
	public void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {  
	    binder.registerCustomEditor(Date.class, new CustomDateEditor(CalendarUtils.MES_ANO_FORMAT , true));  
	    binder.registerCustomEditor(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, MoedaUtil.MOEDA_FORMAT , true));
	} 

	@RequestMapping(value="/new" ,method = RequestMethod.GET)
    public String novo(Map<String, Object> map) 
	{
		Declinio declinio = new Declinio();
		declinio.setIdEntidade(entidadeService.getIdEntidadePadrao(BaseUtil.getBase()));
		
		map.put("declinio", declinio);
		map.put("entidades",entidadeService.listarTodas());
		
		return "frmDeclinio";
	}
	
	@RequestMapping(value="delete/{idDeclinio}", method = RequestMethod.GET)
    public String delete(@PathVariable long idDeclinio, Map<String, Object> map) 
	{
		declinioService.remover(idDeclinio);
		
		return get(map);
	}	
	
	@RequestMapping(method = RequestMethod.GET)
    public String get(Map<String, Object> map) 
	{
		
		List<Declinio> declinios = declinioService.listarTodos(BaseUtil.getBase());
		
		for( Declinio declinio : declinios ){
			declinio.setEntidade( entidadeService.carregarPorId(declinio.getIdEntidade()) );
		}
				
		map.put("declinios",declinios);
		
		return "listDeclinio";
	}
	
	
	@RequestMapping(method = RequestMethod.POST)
	public String salvar( @ModelAttribute("declinio") Declinio declinio , Map<String, Object> map )
	{
		declinio.setBase(BaseUtil.getBase());
		declinioService.salvarDeclinio(declinio);
		
		map.put("mensagemSucesso", "Ameaça salvo com sucesso.");
		
		return novo(map);
	}
	
	
}
